package com.ruoyi.ma.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ma.domain.vo.MaMaterialMeasureQhVo;
import com.ruoyi.ma.domain.bo.MaMaterialMeasureQhBo;
import com.ruoyi.ma.service.IMaMaterialMeasureQhService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 材料计量期号
 *
 * @author llb
 * @date 2024-11-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ma/materialMeasureQh")
public class MaMaterialMeasureQhController extends BaseController {

    private final IMaMaterialMeasureQhService iMaMaterialMeasureQhService;

    /**
     * 查询材料计量期号列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<MaMaterialMeasureQhVo> list(MaMaterialMeasureQhBo bo, PageQuery pageQuery) {
        return iMaMaterialMeasureQhService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出材料计量期号列表
     */
    @SaCheckLogin
    @Log(title = "材料计量期号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MaMaterialMeasureQhBo bo, HttpServletResponse response) {
        List<MaMaterialMeasureQhVo> list = iMaMaterialMeasureQhService.queryList(bo);
        ExcelUtil.exportExcel(list, "材料计量期号", MaMaterialMeasureQhVo.class, response);
    }

    /**
     * 获取材料计量期号详细信息
     *
     * @param measureQhId 主键
     */
    @SaCheckLogin
    @GetMapping("/{measureQhId}")
    public R<MaMaterialMeasureQhVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long measureQhId) {
        return R.ok(iMaMaterialMeasureQhService.queryById(measureQhId));
    }

    /**
     * 新增材料计量期号
     */
    @SaCheckLogin
    @Log(title = "材料计量期号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MaMaterialMeasureQhBo bo) {
        return toAjax(iMaMaterialMeasureQhService.insertByBo(bo));
    }

    /**
     * 修改材料计量期号
     */
    @SaCheckLogin
    @Log(title = "材料计量期号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MaMaterialMeasureQhBo bo) {
        return toAjax(iMaMaterialMeasureQhService.updateByBo(bo));
    }

    /**
     * 删除材料计量期号
     *
     * @param measureQhIds 主键串
     */
    @SaCheckLogin
    @Log(title = "材料计量期号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{measureQhIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] measureQhIds) {
        return toAjax(iMaMaterialMeasureQhService.deleteWithValidByIds(Arrays.asList(measureQhIds), true));
    }


    /**
     * 查询材料计量期号列表不分页
     */
    @SaCheckLogin
    @GetMapping("/allList")
    public R<List<MaMaterialMeasureQhVo>> allList(MaMaterialMeasureQhBo bo) {
        return R.ok(iMaMaterialMeasureQhService.allList(bo));
    }

    // 获取最新期号
    @SaCheckLogin
    @GetMapping("/lastQh")
    public R<Integer> lastQh(MaMaterialMeasureQhBo bo) {
        return R.ok(iMaMaterialMeasureQhService.lastQh(bo));
    }

}
